const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		role: '1',
		id: '',
		name: '',
		goosList: {},
		showBookPicker: false,
		bookList: [],
    orderItem: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	//采购单详情 getOrderInfo
	getDetail(id) {
		wx.$apis.getOrderInfo(id).then(res => {
			wx.hideLoading();
			if (res.data) {
			res.data.goosList= wx.$getYuan(res.data.deliveryPrice)
			res.data.details.map(item=>{
				item.transactionPrice=wx.$getYuan(item.transactionPrice);
				item.countPrice=wx.$getYuan(item.countPrice)
				return item
			})

				this.setData({
					name: res.data.goodsOwnerName,
					tel: res.data.goodsOwnerMobile && wx.$getMobile(res.data.goodsOwnerMobile),
					goosList: res.data,
					"goosList.deliveryPrice": wx.$getYuan(res.data.deliveryPrice),
					"goosList.receiptTime":res.data.receiptTime.replace('T',' ')
				})
			}
		}).catch(err => {
			wx.hideLoading()
		})
	},
	//跳转详情页面
	gotodetail(e) {
		// console.log(e)
		let id = e.currentTarget.dataset.id
		// if(e.currentTarget.dataset.outstockstatus){
			wx.navigateTo({
				url: "/pages/dashboard/hzdetail/hzdetail?id=" + id+"&type=3"
			});
		// }else{
		// 	wx.showToast({
		// 		title: '该订单暂未发货',
		// 		icon: "none",
		// 		duration: 1000
		// 	})
		// }

		// if (this.data.type == 3) {
			//跳转货主详情页面
			// wx.navigateTo({
			// 	url: "/pages/dashboard/hzdetail/hzdetail?id=" + id+"&type=3"
			// });
		// } else {
		// 	//跳转农户详情页面
		// 	wx.navigateTo({
		// 		url: "/pages/dashboard/nhdetail/nhdetail"
		// 	});
		// }
	},
			//获取账本
			getAccountBookEasy() {
				wx.$apis.getAccountBookEasy().then(res => {
					let bookList = res.data && res.data.map(item => {
						item.text = item.name
						return item
					})
					this.setData({
						bookList: bookList
					})
				})
			},
			syncToAccountBook(e) {
				this.setData({
					orderItem: e.currentTarget.dataset.item,
					showBookPicker: true
				})
			},
			onBookCancel() {
				this.setData({
					showBookPicker: false
				})
			},
			onBookConfirm(event) {
				const {
					value
				} = event.detail;
				let params = {  //货主
					accountBookId: value.id,
					financeName: '卖菜',
					recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
					orderType: 1,
					orderId: this.data.orderItem.id,
					type: 0,
				}
				wx.showLoading({
					title: "正在同步"
			})
			wx.$apis.addAccountBookRecord(params).then(res => {
					this.setData({
						showBookPicker: false
					})
					wx.showToast({
						title: '同步完成！', 
						icon: 'success',  
						duration: 1500  
					})
					this.getDetail(this.data.id)
				})
			},
	onLoad: function (options) {
		console.log('optioon', options)
		const id = options.id;
		const name = options.name;
		this.setData({
			id,
			name,
			role: app.globalData.userInfo.role,
		})
		this.getDetail(id)

	},
	//动态获取BarTitle
	getBar: function () {
		wx.setNavigationBarTitle({
			title: this.data.name
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		this.getBar()
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getAccountBookEasy()

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})